/**
 * 
 */
package com.nd.android.payment.business.operateImp;

import android.util.Log;

import org.apache.http.HttpStatus;
import org.json.JSONObject;

import com.common.StarAppConfig;
import com.common.StarAppConst;
import com.common.android.utils.httpRequest.IHttpRequest;
import com.nd.android.payment.R;
import com.nd.android.payment.business.http.PayHttpRequest;
import com.nd.android.payment.controller.Bean.PayConfigBean;
import com.nd.android.payment.controller.Bean.PayInfoBean;
import com.nd.android.payment.controller.Bean.WealthInfoBean;
import com.nd.android.payment.controller.IBean.IPayConfigBean;
import com.nd.android.payment.controller.IBean.IPayInfoBean;
import com.nd.android.payment.controller.IBean.IWealthInfoBean;
import com.nd.android.payment.controller.IOperate.IPayRechargeOperate;
import com.nd.android.payment.util.PaymentUtils;
import com.product.android.business.ApplicationVariable;

/**
 * 
 * <br>Created 2014-11-4 下午5:01:51
 * @version  
 * @author   zhangqr		
 *
 * @see 	 
 */

public class PayRechargeOperateImpl implements IPayRechargeOperate {
    /**
     * 默认成功代码
     */
    public static final String DEFAULT_SUCCESS = "1";
    /**
     * 默认错误代码
     */
    public static final String DEFAULT_ERROR = "0";
    
    /** 星钻兑换金币 */
    private static String URL_PAY_DIAMOND_TO_GOLD = "mg/api/aid/consume";
    
    /** 查询金币和星钻余额 */
    private static String URL_PAY_SEARCH_WEALTH = "mg/api/aid/account_amount";
    
    /**
     * 获取支付配置
     */
    private static String URL_PAY_CONFIG = "mg/api/aid/getitemconf";
    
    public PayRechargeOperateImpl() {
        
    }
    
    /**
     * 中间层请求地址
     * 
     * @return 根据全局变量返回基础地址
     */
    public String getBaseUrl() {
        return StarAppConfig.getConfig(StarAppConfig.MIDDLE_LAYER_FAN_GROUP_SERVICE_KEY);
    }
    
    /**
     * 获得IHttpRequest
     * 
     * <br>Created 2014-11-4 下午8:20:45
     * @return
     * @author       zhangqr
     */
    public IHttpRequest getIHttpRequest() {
        IHttpRequest iHttpRequest = PayHttpRequest.getInstance();
        return iHttpRequest;
    }
    
    /**
     * 获取充值配置
     * 
     * <br>Created 2014-11-13 上午10:09:20
     * @return
     * @author       zhangqr
     */
    public IPayConfigBean getPayConfig() {
        IPayConfigBean payConfig = new PayConfigBean();
        payConfig.setErrCode(DEFAULT_ERROR);
        try {
            StringBuilder url = new StringBuilder();
            url.append(getBaseUrl()).append(URL_PAY_CONFIG);
            
            StringBuilder receive = new StringBuilder();
            
            int iRet = getIHttpRequest().doGet(url.toString(), receive);
            if (iRet == HttpStatus.SC_OK) {
                JSONObject obj = new JSONObject(receive.toString());
                if(obj != null) {
                    
                    if (obj.has("code") && obj.has("msg")){
                        payConfig.setErrCode(obj.optString("code", DEFAULT_ERROR+""));
                        payConfig.setErrMsg(_getResponseErrMsg(receive));
                        
                        return payConfig;
                    }
                    
                    payConfig.setExChangeRate(obj.optInt("exchange_rate"));
                    
                    JSONObject item = obj.optJSONObject("item");
                    
                    if(item != null) {
                        payConfig.setErrCode(DEFAULT_SUCCESS);
                        
                        payConfig.setId(item.optString("id"));
                        payConfig.setName(item.optString("name"));
                        payConfig.setPrice(item.optDouble("price"));
                        
                        return payConfig;
                    }
                }
            } else {
                payConfig.setErrCode(iRet+"");
                payConfig.setErrMsg(
                        ApplicationVariable.INSTANCE.applicationContext
                        .getString(R.string.pay_http_failed));
                
                return payConfig;
            }
        } catch (Exception e) {
            e.printStackTrace();
            
            payConfig.setErrCode(DEFAULT_ERROR);
            payConfig.setErrMsg(e.getMessage());
        }
        return payConfig;
    }
    
    /**
     * 星钻兑换金币
     * 
     * @param diamondCount  兑换的星钻数量
     * @return
     */
    public IPayInfoBean diamondToGold(int diamondCount) {
        IPayInfoBean payInfo = new PayInfoBean();
        payInfo.setErrCode(DEFAULT_ERROR);
        try {
            int rate = StarAppConst.PAY.DEFAULT_RATE;
            try{
                rate = Integer.parseInt(StarAppConfig.getConfig(StarAppConfig.CONFIG_PAY_RATE));
            } catch(Exception e) {
                rate = StarAppConst.PAY.DEFAULT_RATE;
            }
            
            StringBuilder url = new StringBuilder();
            url.append(getBaseUrl()).append(URL_PAY_DIAMOND_TO_GOLD);
            
            JSONObject json = new JSONObject();
            json.put("stream_id", PaymentUtils.getUUID());
            json.put("currency", 1);
            json.put("amount", diamondCount);
            json.put("note", String.format(
                    ApplicationVariable.INSTANCE.applicationContext
                        .getString(R.string.pay_buy_gold_order_remark), 
                        diamondCount+"", diamondCount * rate)
                    );
            
            StringBuilder receive = new StringBuilder();
            
            int iRet = getIHttpRequest().doPost(url.toString(), json, receive);
            if (iRet == HttpStatus.SC_OK) {
                JSONObject obj = new JSONObject(receive.toString());
                if(obj != null) {
                    if (obj.has("code") && obj.has("msg")){
                        payInfo.setErrCode(obj.optString("code", DEFAULT_ERROR+""));
                        payInfo.setErrMsg(_getResponseErrMsg(receive));
                        
                        return payInfo;
                    }
                    
                    payInfo.setErrCode(DEFAULT_SUCCESS);
                    
                    payInfo.setGolden(obj.optInt("golden", 0));
                    payInfo.setStar(obj.optInt("star", 0));
                    
                    return payInfo;
                }
            } else {
                payInfo.setErrCode(iRet+"");
                payInfo.setErrMsg(
                        ApplicationVariable.INSTANCE.applicationContext
                        .getString(R.string.pay_http_failed));
                
                return payInfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
            
            payInfo.setErrCode(DEFAULT_ERROR);
            payInfo.setErrMsg(e.getMessage());
        }
        return payInfo;
    }
    
    /**
     * 查询星钻、金币余额
     * 
     * @return
     */
    public IWealthInfoBean getWealthInfo() {
        IWealthInfoBean wealthInfo = new WealthInfoBean();
        wealthInfo.setErrCode(DEFAULT_ERROR);
        try {
            StringBuilder url = new StringBuilder();
            url.append(getBaseUrl()).append(URL_PAY_SEARCH_WEALTH);
            
            StringBuilder receive = new StringBuilder();
            
            int iRet = getIHttpRequest().doGet(url.toString(), receive);
            if (iRet == HttpStatus.SC_OK) {
                JSONObject obj = new JSONObject(receive.toString());
                if(obj != null) {
                    if (obj.has("code") && obj.has("msg")){
                        wealthInfo.setErrCode(obj.optString("code", DEFAULT_ERROR+""));
                        wealthInfo.setErrMsg(_getResponseErrMsg(receive));
                        
                        return wealthInfo;
                    }
                    
                    wealthInfo.setErrCode(DEFAULT_SUCCESS);
                    
                    wealthInfo.setGolden(obj.optInt("golden", 0));
                    wealthInfo.setStar(obj.optInt("star", 0));
                    
                    return wealthInfo;
                }
            } else {
                wealthInfo.setErrCode(iRet+"");
                wealthInfo.setErrMsg(
                        ApplicationVariable.INSTANCE.applicationContext
                        .getString(R.string.pay_http_failed));
                
                return wealthInfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
            
            wealthInfo.setErrCode(DEFAULT_ERROR);
            wealthInfo.setErrMsg(e.getMessage());
        }
        return wealthInfo;
    }
    
    /**
     * 解析错误信息
     * 
     * @param receive
     * @return
     */
    private String _getResponseErrMsg(StringBuilder receive) {
        String msg = "";
        try {
            JSONObject json = new JSONObject(receive.toString());
            if (json.has("msg")) {
                msg = json.optString("msg");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

}
